/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165;

import java.util.List;
import javax.ejb.Local;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;

/**
 *
 * @author Matúš
 */
@Stateless
//@Local(value=ReaderDAOLocal.class)
public class ReaderDAO implements ReaderDAOLocal {

    @PersistenceContext
    private EntityManager em;
    
    private void checkParameters(Reader reader) {
        if (reader == null) {
            throw new IllegalArgumentException("Reader cannot be null.");
        }
        if (reader.getFirstName() == null) {
            throw new IllegalArgumentException("Reader's first name cannot be null.");
        }
        if (reader.getSurname() == null) {
            throw new IllegalArgumentException("Reader's surname cannot be null.");
        }
        if (reader.getBirthNumber() == null) {
            throw new IllegalArgumentException("Reader's birth number cannot be null.");
        }
        if (reader.getAddress() == null) {
            throw new IllegalArgumentException("Reader's first name cannot be null.");
        }
        if (reader.getEmail() == null) {
            throw new IllegalArgumentException("Reader's email cannot be null.");
        }
        if (reader.getFirstName().compareTo("") == 0) {
            throw new IllegalArgumentException("Reader's first name cannot be empty.");
        }
        if (reader.getSurname().compareTo("") == 0) {
            throw new IllegalArgumentException("Reader's surname cannot be empty.");
        }
        if (reader.getBirthNumber().compareTo("") == 0) {
            throw new IllegalArgumentException("Reader's birth number cannot be empty.");
        }
        if (reader.getAddress().compareTo("") == 0) {
            throw new IllegalArgumentException("Reader's address cannot be empty.");
        }
        if (reader.getEmail().compareTo("") == 0) {
            throw new IllegalArgumentException("Reader's email cannot be empty.");
        }
    }
    
    @Override
    public Reader createReader(Reader reader) {
        checkParameters(reader);
        if (reader.getId() != null) {
            throw new IllegalArgumentException("Reader id is already set.");
        }
        
        em.persist(reader);
        return reader;
    }

    @Override
    public List<Reader> findAllReaders() {
        List<Reader> result = em.createQuery("SELECT e FROM Reader e", Reader.class).getResultList();
        return result;
    }
    
    @Override
    public Reader findReaderByEmail(String email) {
        if (email == null) {
            throw new IllegalArgumentException("Email cannot be null.");
        }
        if (email.compareTo("") == 0) {
            throw new IllegalArgumentException("Email cannot be empty.");
        }

        String query = "SELECT e FROM Reader e WHERE e.email = '" + email + "'";
        try {
            return em.createQuery(query, Reader.class).getSingleResult();
        } catch (NoResultException nre) {
            return null;
        } catch (NonUniqueResultException nure) {
            throw new NonUniqueResultException("There are more readers with this email: " + email);
        }
    }
    
    @Override
    public void updateReader(Reader reader) {
        checkParameters(reader);

        Reader reader2 = em.find(Reader.class, reader.getId());
        if (reader2 != null) {
            reader2.setFirstName(reader.getFirstName());
            reader2.setSurname(reader.getSurname());
            reader2.setBirthNumber(reader.getBirthNumber());
            reader2.setEmail(reader.getEmail());
            reader2.setAddress(reader.getAddress());
            reader2.setTelephoneNumber(reader.getTelephoneNumber());
        } else {
            throw new IllegalArgumentException("Unknown reader id: " + reader.getId());
        }
        em.merge(reader2);
    }
    
    @Override
    public void deleteReader(Reader reader) {
        if (reader == null) {
            throw new IllegalArgumentException("Deleted reader cannot be null");
        }
        if (reader.getId() == null) {
            throw new IllegalArgumentException("Unknown reader - has null id.");
        }

        Reader r = em.merge(reader);
        em.remove(r);
    }
}
